package com.imonsoft.sqm.service.excel.SupplierExcel;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.imonsoft.common.constant.ConstantDict;
import com.imonsoft.common.util.ValidatorUtil;
import com.imonsoft.sqm.pojo.basedata.Dictionary;
import com.imonsoft.sqm.pojo.basedata.Supplier;
import com.imonsoft.sqm.service.basedata.DictionaryService;
import com.imonsoft.sqm.service.basedata.SupplierService;
import com.imonsoft.sqm.web.excel.SupplierExcel.SupplierImportDto;

@Service("supplierExcelService")
public class SupplierExcelServiceImpl implements SupplierExcelService{
	@Autowired
	private DictionaryService dictionaryService;
	@Autowired
	private SupplierService supplierService;
	/**
	 * 检查单行数据
	 * @param supplierImportDto
	 * @param setCode
	 * @return
	 */
	public List<String> txCheckData(SupplierImportDto supplierImportDto, Map<Integer, String> setCode) {
		List<String> messages = new ArrayList<String>();
		List<Dictionary> materialGroupList = dictionaryService.findDictionaryByType(ConstantDict.SUPPLIER_MATERIAL_GROUP);
		if (materialGroupList == null || materialGroupList.size() <= 0) {
			messages.add("系统中不存在材料组，请先维材料组数据");
		}
		if (StringUtils.isEmpty(supplierImportDto.getCode())) {
			messages.add("第[" + supplierImportDto.getRow() + "]行供应商编号不能为空!");
		} else {
			Supplier supplier = new Supplier();
			supplier.setCode(supplierImportDto.getCode());
			List<Supplier> checkHave = supplierService.findList(supplier);
			if (checkHave != null && checkHave.size() > 0) {
				messages.add("第[" + supplierImportDto.getRow() + "]行供应商编号系统已经存在!");
			}
		}
		if (setCode != null) {
			@SuppressWarnings("rawtypes")
			Iterator it = setCode.entrySet().iterator();
			while (it.hasNext()) {
				@SuppressWarnings("rawtypes")
				Map.Entry e = (Map.Entry) it.next();
				if (supplierImportDto.getCode().equals(e.getValue()) && supplierImportDto.getRow() != Integer.parseInt(e.getKey() + "")) {
					messages.add("第[" + supplierImportDto.getRow() + "]行与" + "第[" + e.getKey() + "]行" + "供应商编码重复!");
					break;
				}
			}
		}
		if (StringUtils.isEmpty(supplierImportDto.getName())) {
			messages.add("第[" + supplierImportDto.getRow() + "]行供应商名称不能为空!");
		}
		if (StringUtils.isEmpty(supplierImportDto.getMail())) {
			messages.add("第[" + supplierImportDto.getRow() + "]行供应商Email不能为空!");
		} else {
			if (!ValidatorUtil.isEmail(supplierImportDto.getMail())) {
				messages.add("第[" + supplierImportDto.getRow() + "]行邮箱格式错误!");
			}
		}
		if (StringUtils.isNotEmpty(supplierImportDto.getPhone())) {
			if (!ValidatorUtil.isTelephone(supplierImportDto.getPhone()) && !ValidatorUtil.isHandset(supplierImportDto.getPhone())) {
				messages.add("第[" + supplierImportDto.getRow() + "]行电话格式错误!");
			}
		}
		if (StringUtils.isEmpty(supplierImportDto.getMaterialGroupName())) {
			messages.add("第[" + supplierImportDto.getRow() + "]行供材料组不能为空!");
		} else {
			if (materialGroupList != null && materialGroupList.size() > 0) {
				for (Dictionary d : materialGroupList) {
					if (d.getName().equals(supplierImportDto.getMaterialGroupName())) {
						supplierImportDto.setMaterialGroup(d.getCode());
						break;
					}
				}
			}
			if (supplierImportDto.getMaterialGroup() == null) {
				messages.add("第[" + supplierImportDto.getRow() + "]行材料组系统中不存在!");
			}
		}
		return messages;
	}
}
